package com.yun.web;

import com.yun.pojo.User;
import com.yun.service.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user/home")
public class loginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String uname = request.getParameter("usercode");
        String pwd = request.getParameter("pwd");

        UserServiceImp serviceImp = new UserServiceImp();
        User user = serviceImp.queryUser(uname, pwd);

        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        if(user!=null){//证明查询到账号和密码正确:可以登录
            session.setAttribute("uname",user.getUsername());
            //重定向
            response.sendRedirect("/home");
        }else {
            //输入的账号或密码不存在，提示用户并返回到登录页面
            request.setAttribute("msg","用户名或密码不正确，请重新输入！");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);

        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
